// miniprogram/pages/consumption/month/month.js
const db = wx.cloud.database()
const _ = db.command
const $ = _.aggregate
Page({
  data: {
    list: null,
    month: null,
    page: 1,
    isLoading: false,
    noData: false
  },
  onLoad: function (options) {
    this.setData({
      month: options.month
    })
    wx.showLoading({
      title: '正在加载...'
    })
  },
  onShow: function () {
    this.initData()
  },
  initData: function (isLoading) {
    let page = this.data.page
    if(isLoading) {
      page += 1
      if(!this.data.noData) {
        this.setData({
          isLoading: true
        })
      }
    }else{
      page = 1
    }
    wx.cloud.callFunction({
      name: 'consume',
      data: {
        action: 'consume_month',
        month: this.data.month,
        page: page
      },
      success: res => {
        wx.hideLoading()
        this.setData({
          isLoading: false
        })
        if(res.result.length == 0) {
          this.setData({
            noData: true
          })
          return
        }
        if(isLoading) {
          let list = this.data.list
          list = list.concat(res.result)
          this.setData({
            list: list,
            page: page
          })
        }else{
          this.setData({
            list: res.result,
            page: page
          })
        }
      },
      fail: err => {
        wx.hideLoading()
      }
    })
  },
  go: function (e) {
    let url = e.currentTarget.dataset.url
    wx.navigateTo({
      url: url
    })
  },
  onReachBottom () {
    this.initData(true)
  }
})